//
//  LBCReadHeadView.m
//  Moment
//
//  Created by dllo on 15/11/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCReadHeadView.h"

@interface LBCReadHeadView ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *headScrollV;
@property (nonatomic, retain)UIPageControl *headPage;
@property (nonatomic, retain)NSMutableArray *imageArray;
@property (nonatomic, retain)NSMutableArray *urlArray;

@end

@implementation LBCReadHeadView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    self.backgroundColor = [UIColor whiteColor];
    self.headScrollV = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.headScrollV.backgroundColor = [UIColor grayColor];
    [self addSubview:self.headScrollV];
    
    self.imageArray = [NSMutableArray array];
    self.urlArray = [NSMutableArray array];
    
    self.headScrollV.delegate = self;
    // 按页滑动
    self.headScrollV.pagingEnabled = YES;
    // 取消边界反弹效果
    self.headScrollV.bounces = NO;
    // 是否显示水平滑动条
    self.headScrollV.showsHorizontalScrollIndicator = NO;
    
    
    // 设置页码
    self.headPage = [[UIPageControl alloc] initWithFrame:CGRectMake(self.headScrollV.frame.size.width / 1.6, self.headScrollV.frame.size.height - self.headScrollV.frame.size.height / 5, self.headScrollV.frame.size.width / 2, self.headScrollV.frame.size.height / 6)];
    self.headPage.backgroundColor = [UIColor clearColor];
    [self addSubview:self.headPage];
    // 页码个数
    self.headPage.numberOfPages = 3;
    // 从第几个开始显示
    self.headPage.currentPage = 0;
#pragma mark- 设置定时器
    // 参数1.时间
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changePhoto:) userInfo:@"time" repeats:YES];
}



-(void)setReadArray:(NSMutableArray *)readArray{
    self.headPage.numberOfPages = readArray.count;
        _readArray = readArray;
    NSMutableArray *tempArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in self.readArray) {
            [tempArray addObject:[dic objectForKey:@"img"]];
            [self.urlArray addObject:[dic objectForKey:@"url"]];
        }
    if (tempArray.count != 0) {
    
        [self.imageArray addObject:tempArray.lastObject];
        [self.imageArray addObjectsFromArray:tempArray];
        [self.imageArray addObject:tempArray.firstObject];
        self.headScrollV.contentSize = CGSizeMake(self.bounds.size.width * self.imageArray.count, 180);
    }
    for (int i = 0; i < self.imageArray.count; i++) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(self.headScrollV.frame.size.width * i, 0, self.headScrollV.frame.size.width, self.headScrollV.frame.size.height)];
        NSURL *urlImg = [NSURL URLWithString:[self.imageArray objectAtIndex:i]];
        [img sd_setImageWithURL:urlImg placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
        [self.headScrollV addSubview:img];
        img.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [img addGestureRecognizer:tap];
    }
    // 初始偏移量
    self.headScrollV.contentOffset = CGPointMake(self.bounds.size.width, 0);
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    for (int i = 0; i < self.urlArray.count; i++) {
        if (self.headPage.currentPage == i) {
        NSString *tempStr = [self.urlArray objectAtIndex:i];
        self.strId = [tempStr substringWithRange:NSMakeRange(17, 24)];
            [self.readHeadDelegate presentToViewControllerContentId:self.strId];
        }
    }
}




- (void)newsButtonAction:(UIButton *)btn{
    
}

#pragma mark- 减速停止时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (self.headScrollV.contentOffset.x == SCREEN_WIDTH * (self.imageArray.count - 1)) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    } else if (self.headScrollV.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (self.imageArray.count - 2), 0);
    }
    
    self.headPage.currentPage = (scrollView.contentOffset.x - SCREEN_WIDTH) / SCREEN_WIDTH;
    
}
#pragma mark- 定时器方法实现
-(void)changePhoto:(NSTimer *)time{
    // 记录开始的偏移量
    [UIView animateWithDuration:0.5 animations:^{
        
        CGPoint tempContentSize = self.headScrollV.contentOffset;
        tempContentSize.x += SCREEN_WIDTH;
        self.headScrollV.contentOffset = tempContentSize;
        
        UIScrollView *tempSV = [[UIScrollView alloc] init];
        tempSV.contentOffset = self.headScrollV.contentOffset;
    }];
    if(self.headScrollV.contentOffset.x == (self.imageArray.count - 1) * SCREEN_WIDTH){
        self.headScrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    } else if(self.headScrollV.contentOffset.x == 0){
        self.headScrollV.contentOffset = CGPointMake(SCREEN_WIDTH * (self.imageArray.count - 2), 0);
    }
    self.headPage.currentPage  = (self.headScrollV.contentOffset.x - SCREEN_WIDTH) / SCREEN_WIDTH ;
}




@end
